package com.example.mydownloader.ui.main

import android.os.Bundle
import androidx.appcompat.app.AppCompatActivity
import androidx.lifecycle.ViewModelProvider
import com.example.mydownloader.R
import com.example.mydownloader.databinding.ActivityMainBinding
import com.example.mydownloader.mymodule.log.LogUtil
import com.example.mydownloader.netty.NettyManager

class MainActivity : AppCompatActivity() {

    private lateinit var mainViewModel: MainViewModel
    private lateinit var binding: ActivityMainBinding

    private var userId: String? = null
    private var displayName: String? = null
    private var token: String? = null
    private var nettyUrls: ArrayList<String>? = null

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        binding = ActivityMainBinding.inflate(layoutInflater)
        setContentView(binding.root)
        mainViewModel = ViewModelProvider(
            this, MainViewModelFactory()
        )
            .get(MainViewModel::class.java)

        //Netty连接
        userId = intent.getStringExtra("userId")
        displayName = intent.getStringExtra("displayName")
        token = intent.getStringExtra("myToken")
        nettyUrls = intent.getStringArrayListExtra("nettyUrls")
        //getNettyConfig
        if (token != null) {
            if (nettyUrls != null) {
                startNetty(token!!, nettyUrls!!)
            } else {
                LogUtil.w("nettyUrls is empty.can not start netty.")
            }
        } else {
            LogUtil.w("token is empty.can not start netty.")
        }
    }

    private fun startNetty(token: String, nettyUrls: ArrayList<String>) {
        NettyManager.setSome(token, nettyUrls).start()
    }


}